package cn.itsource.controller;

import cn.itsource.bean.Employee;
import cn.itsource.query.EmpQuery;
import cn.itsource.service.IEmployeeService;
import cn.itsource.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author : 张金铭
 * @description :
 * @create :2022-11-08 10:45:00
 */
@Slf4j
@RestController
@RequestMapping("/emp")
public class EmployeeController {

    @Resource
    private IEmployeeService employeeService;

    @RequestMapping("/getAll")
    public JsonResult getAll(){
        return JsonResult.success(employeeService.list());
    }


    @GetMapping("/{id}")
    public JsonResult getEmp(@PathVariable("id") Long id){

        return employeeService.getEmp(id);
    }


    @PostMapping("/findPage")
    public JsonResult findPage(@RequestBody EmpQuery query){
//        log.error(query.toString());
        return employeeService.getPage(query);
    }

    @PostMapping("/add")
    public JsonResult addOrUpdate(@RequestBody Employee employee){
//        log.error(shop.toString());
        if (employee.getId()==null){
            employeeService.save(employee);
            return JsonResult.success("新增成功");
        }else {
            employeeService.updateById(employee);
            return JsonResult.success("修改成功");
        }
    }

    @PostMapping("/{id}")
    public JsonResult deleteById(@PathVariable("id") Long id){
//        log.error(id.toString());
        employeeService.removeById(id);
        return JsonResult.success("删除成功");
    }
}
